package com.crazymaker.agent.demo.bytebuddy;

import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

import static com.crazymaker.agent.demo.bytebuddy.CostTimeTracer.currentSpan;
import static com.crazymaker.agent.demo.bytebuddy.CostTimeTracer.newSpan;

public class CostTimeAspect {

    @RuntimeType
    public static Object intercept(@Origin Method method,
                                   @SuperCall Callable<?> callable) throws Exception {
        newSpan(method).start();

        long start = System.currentTimeMillis();

        try {
            // 原有函数执行
            return callable.call();
        } finally {

            currentSpan().end();

            System.out.println(method + " $$  方法耗时：" + (System.currentTimeMillis() - start) + "ms");
        }
    }

}